package 力扣._00周赛._424周赛.问题1;

import java.util.Arrays;

class Solution {
    public int countValidSelections(int[] nums) {
        int n = nums.length;
        int total = 0;

        for (int i = 0; i < n; i++) {
            if (nums[i] == 0) {
                // Try moving to the left
                if (isValid(nums.clone(), i, -1)) {
                    total++;
                }
                // Try moving to the right
                if (isValid(nums.clone(), i, 1)) {
                    total++;
                }
            }
        }

        return total;
    }

    private boolean isValid(int[] nums, int curr, int direction) {
        int n = nums.length;

        while (curr >= 0 && curr < n) {
            if (nums[curr] == 0) {
                curr += direction;
            } else if (nums[curr] > 0) {
                nums[curr]--;
                direction = -direction;
                curr += direction;
            }
        }

        return Arrays.stream(nums).sum() == 0;
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] nums = {0, 1, 2, 0, 1};
        System.out.println(solution.countValidSelections(nums)); // Output the number of valid selections
    }
}